package com.example.woke;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.http.HttpMethods;
import com.zhongcai.online.R;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class ChangephoneActivity extends Activity {

	private TextView mTcode;
	private ProgressBar mBar;
	private EditText mEdit,mEdit2;
	private TextView mTok;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_changephone);

		intview();

	}
	
	private void getdatacode(String phone) {
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(
				ChangephoneActivity.this);
		ahc.setCookieStore(myCookieStore);
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "Public");
		params.put("a", "sendCode");
		params.put("username", phone);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String flag = response.getString("flag");
					if (flag.equals("success")) {
						Toast.makeText(ChangephoneActivity.this, "验证码已发送！",
								Toast.LENGTH_SHORT).show();
						mTcode.setBackgroundDrawable(getResources()
								.getDrawable(R.drawable.rect_lightgray));
					} else {
						mTcode.setClickable(true);
						mTcode.setBackgroundDrawable(getResources()
								.getDrawable(R.drawable.rect_lightred));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Log.e("response", "失败" + responseString);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
				mTcode.setClickable(false);
				mTcode.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.rect_lightgray));
				// Message msg = new Message();
				// msg.what = 1;//��������
				// mHandler.sendMessage(msg);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mTcode.setClickable(true);
				mBar.setVisibility(View.GONE);
				// Message msg = new Message();
				// msg.what = 2;//�رն���
				// mHandler.sendMessage(msg);
			}
		});
	}

	private void senddatacode(String phone,String code) {
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(
				ChangephoneActivity.this);
		ahc.setCookieStore(myCookieStore);
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "updataLogin");
		params.put("username", phone);
		params.put("code", code);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				
				mTok.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.rect_lightred));
				
				Log.e("username", ""+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if (flag.equals("success")) {
						Toast.makeText(ChangephoneActivity.this, ""+info,
								Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(ChangephoneActivity.this, ""+info,
								Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Log.e("response", "修改败" + responseString);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
				mTok.setClickable(false);
				mTok.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.rect_lightgray));
				// Message msg = new Message();
				// msg.what = 1;//��������
				// mHandler.sendMessage(msg);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mTok.setClickable(true);
				mBar.setVisibility(View.GONE);
				// Message msg = new Message();
				// msg.what = 2;//�رն���
				// mHandler.sendMessage(msg);
			}
		});
	}
	
	private TextWatcher watcher = new TextWatcher() {
	    @Override
	    public void onTextChanged(CharSequence s, int start, int before, int count) {
	    }
	    @Override
	    public void beforeTextChanged(CharSequence s, int start, int count,
	            int after) {
	    }
	    
	    @Override
	    public void afterTextChanged(Editable s) {
	    	String trim = ""+mTcode.getText().toString().trim();
	    	if(trim.equals("")){
	    		mTok.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightgray));
	    	}else{
	    		mTok.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightred));
	    	}
	    }
	};

	private void intview() {

		mTcode = (TextView) findViewById(R.id.change_phone_text);
		mEdit = (EditText) findViewById(R.id.change_phone_e1);
		mEdit2 = (EditText) findViewById(R.id.change_phone_e2);
		mTok = (TextView) findViewById(R.id.change_phone_ok);
		mTcode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String trim = "" + mEdit.getText().toString().trim();
				if (trim.equals("")) {
					Toast.makeText(ChangephoneActivity.this, "请输入手机号",
							Toast.LENGTH_SHORT).show();
				} else {
					getdatacode(trim);
				}
			}
		});
		mEdit2.addTextChangedListener(watcher);
		
		mBar = (ProgressBar) findViewById(R.id.change_phone_bar);
		findViewById(R.id.avuserphone_image_back).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});

		mTok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String trim = "" + mEdit.getText().toString().trim();
				String trim1 = "" + mTcode.getText().toString().trim();
				if (trim.equals("")) {
					Toast.makeText(ChangephoneActivity.this, "请输入手机号",
							Toast.LENGTH_SHORT).show();
				} else if(trim1.equals("")){
					Toast.makeText(ChangephoneActivity.this, "请输入验证码",
							Toast.LENGTH_SHORT).show();
				}else{
					senddatacode(trim,trim1);
				}
			}
		});
	}
}
